package test.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.item.ChunkOrientedTasklet;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.database.Order;
import org.springframework.batch.item.database.support.MySqlPagingQueryProvider;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import test.pojo.Person;
import test.pojo.SysLog;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : example
 * @ Author: XuLeHuang
 * @ Date: 2022/8/2 14:46
 * @ Description:
 */
@Configuration
@EnableBatchProcessing
public class HandleDateFromDbToFileConfig {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;


    @Autowired
    @Qualifier("dbJdbcReader")
    private ItemReader<? extends Person> dbJdbcReader;

    @Autowired
    @Qualifier("fileItemWriter")
    private ItemWriter<? super Person> fileItemWriter;

    @Autowired
    @Qualifier("itemProcessor")
    private ItemProcessor<Person , Person> itemProcessor;

    @Bean
    public Job handleDateFromDbToFileJob() {
        return jobBuilderFactory.get("handleDateFromDbToFileJobV27")
                .start(handleDateFromDbToFileStep())
                .build();
    }

    @Bean
    public Step handleDateFromDbToFileStep() {
        return stepBuilderFactory.get("itemReadDbStep")
                //目前测试3441条数据，对半左右分处理速度最快
                .<Person , Person>chunk(2500000)
                .reader(dbJdbcReader)
                .processor(itemProcessor)
                .writer(fileItemWriter)
                .build();
    }

    /*@Bean
    @StepScope
    public ItemReader<SysLog> dbJdbcReader() {
        JdbcPagingItemReader<SysLog> sysLogJdbcPagingItemReader = new JdbcPagingItemReader<>();
        sysLogJdbcPagingItemReader.setDataSource(dataSource);
        sysLogJdbcPagingItemReader.setFetchSize(2);
        sysLogJdbcPagingItemReader.setRowMapper(new RowMapper<SysLog>() {
            @Override
            public SysLog mapRow(ResultSet rs, int rowNum) throws SQLException {
                SysLog sysLog = new SysLog();
                return sysLog.setId(rs.getString(1))
                        .setUserName(rs.getString(2))
                        .setOperation(rs.getString(3))
                        .setTime(rs.getString(4))
                        .setMethod(rs.getString(5))
                        .setParams(rs.getString(6))
                        .setIp(rs.getString(7))
                        .setDelFlag(rs.getString(8));
            }
        });
        MySqlPagingQueryProvider provider = new MySqlPagingQueryProvider();
        provider.setSelectClause("id , user_name , operation , time , method , params , ip  , del_flag");
        provider.setFromClause("from t_sys_log");
        HashMap<String, Order> sort = new HashMap<>(1);
        sort.put("id" , Order.ASCENDING);
        provider.setSortKeys(sort);
        sysLogJdbcPagingItemReader.setQueryProvider(provider);
        return sysLogJdbcPagingItemReader;
    }*/

   /* @Bean
    @StepScope
    public ItemWriter<SysLog> fileWriter() {
        FlatFileItemWriter<SysLog> sysLogFlatFileItemWriter = new FlatFileItemWriter<>();
        sysLogList.forEach(sysLog -> System.out.println(sysLog));
        return sysLogFlatFileItemWriter;
    }*/

}
